<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :tree-node="company" :is-root="true" />
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <tree-tools slot-scope="{ data }" :tree-node="data" @addDepts="addDepts" @delDepts="getDepartments" />
        </el-tree>
      </el-card>
      <add-dept :show-dialog.sync="showDialog" :tree-node="node" @addDept="getDepartments" />
    </div>
  </div>
</template>

<script>
import treeTools from '@/views/departments/components/tree-tools.vue'
import { getDepartmentsApi } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import addDept from '@/views/departments/components/add-dept.vue'
export default {
  name: 'Department',
  components: {
    treeTools,
    addDept,
  },
  data() {
    return {
      company: { name: 'kky教育科技股份有限公司', manager: '负责人' },
      departs: [],
      defaultProps: {
        label: 'name',
      },
      showDialog: false,
      node: null,
    }
  },
  created() {
    this.getDepartments()
  },
  mounted() {},
  methods: {
    async getDepartments() {
      const res = await getDepartmentsApi()
      this.departs = tranListToTreeData(res.depts, '')
    },
    addDepts(node) {
      this.showDialog = true
      this.node = node
    },
  },
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
